package com.macrolab.secure.softhsm;

import org.bouncycastle.jce.PKCS10CertificationRequest;
import org.bouncycastle.openssl.jcajce.JcaPEMWriter;
import org.bouncycastle.util.io.pem.PemObject;

import javax.security.auth.x500.X500Principal;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;


/**
 * Generation of a basic PKCS #10 request.
 */

public class PKCS10CertRequestExample {
    public static PKCS10CertificationRequest generateRequest(

            KeyPair pair)

            throws Exception {
        return new PKCS10CertificationRequest(

                "SHA256withRSA",

                new X500Principal("C=CN,ST=上海,L=上海,O=火星,OU=北极,CN=超人"),

                pair.getPublic(),

                null,

                pair.getPrivate());

    }


//    public static void main(String[] args) throws Exception {
//        // create the keys
//        KeyPair kp = generateKeyPair();
//        X509Certificate cert  = caCertGen(kp);
//        System.out.println(cert);
//        savePrivateKey(kp.getPrivate(),"cert/privSm2.pri");
//        String pemCertString = saveX509ToPemFile(cert,"cert/certSm2.crt");
//        System.out.println(pemCertString);
//
//
//
//
//        KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", "BC");
//
//        kpGen.initialize(1024, SecureRandom.getInstanceStrong());
//
//        KeyPair pair = kpGen.generateKeyPair();
//
//        PKCS10CertificationRequest request = generateRequest(pair);
//
//        PEMWriter pemWrt = new PEMWriter(new OutputStreamWriter(System.out));
//
//        pemWrt.writeObject(request);
//
//        pemWrt.close();
//
//    }

}

